const KoaRouter = require('koa-router');
const HttpClient = require('../util/HttpClient');
const Template = require('../util/Template');

const router = new KoaRouter();
const modules = require('../../modules');

// 合同列表
router.get('/contract', async ctx => {
  let script = ['/contract.list.js'];
  let modConfig = modules['contract.list'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/contract', 'get', null, {
    headers: reqHeaders,
  });
  let body = result.body;
  ctx.body = await Template.renderWithHtml(
    '合同管理',
    body,
    script,
    modConfig.template,
    ctx
  );
});

// 新建合同列表
router.get('/contract/create', async ctx => {
  let script = ['/contract.create.js'];
  let modConfig = modules['contract.create'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/contract/create', 'get', null, {
    headers: reqHeaders,
  });
  let body = result.body;
  ctx.body = await Template.renderWithHtml(
    '新建合同',
    body,
    script,
    modConfig.template,
    ctx
  );
});

// 编辑合同列表
router.get('/contract/edit/:id', async ctx => {
  let script = ['/contract.edit.js'];
  let modConfig = modules['contract.edit'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request(
    `/contract/edit/${ctx.params.id}`,
    'get',
    null,
    {
      headers: reqHeaders,
    }
  );
  let body = result.body;
  ctx.body = await Template.renderWithHtml(
    '编辑合同',
    body,
    script,
    modConfig.template,
    ctx
  );
});

// 合同详情
router.get('/contract/view/:id', async ctx => {
  let script = ['/contract.view.js'];
  let modConfig = modules['contract.view'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request(
    `/contract/view/${ctx.params.id}`,
    'get',
    null,
    {
      headers: reqHeaders,
    }
  );
  let body = result.body;
  ctx.body = await Template.renderWithHtml(
    '合同详情',
    body,
    script,
    modConfig.template,
    ctx
  );
});

module.exports = router;
